package com.zycoin.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * @author zhongyicheng
 * create date 2018/1/11.
 */
public class Reactor {
    private Selector selector;
    private ServerSocketChannel serverSocket;

    public Reactor(int port) throws IOException {
        selector = Selector.open();
        serverSocket = ServerSocketChannel.open();
        serverSocket.socket().bind(
                new InetSocketAddress(port));
        serverSocket.configureBlocking(false);
        SelectionKey sk =
                serverSocket.register(selector,
                        SelectionKey.OP_ACCEPT);
        sk.attach(new Acceptor(serverSocket));
        new Thread(new Dispatch(selector)).start();
    }


    public static void main(String[] args) {
        try {
            new Reactor(9000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
